16. Solution: Java Object Serialization

Solution: Java Object Serialization

ND079 JPND C2 L02 A16 Solution Serializing Data In Java V2

UdacisearchClient.java

public final class UdacisearchClient implements Serializable {
  ... // Everything else stays the same!
}

Main.java

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.time.Instant;
import java.time.ZoneId;

public final class Main {
  public static void main(String[] args) throws Exception {
    if (args.length != 1) {
      System.out.println("Usage: Main [file path]");
      return;
    }

    UdacisearchClient client =
        new UdacisearchClient(
            "CatFacts LLC",
            17,
            8000,
            5,
            Instant.now(),
            Duration.ofDays(180),
            ZoneId.of("America/Los_Angeles"),
            "555 Meowmers Ln, Riverside, CA 92501");

    Path outputPath = Path.of(args[0]);
    try (ObjectOutputStream out = new ObjectOutputStream(Files.newOutputStream(outputPath))) {
      out.writeObject(client);
    }
    System.out.println("Wrote to " + outputPath.toAbsolutePath().toString());

    try (ObjectInputStream in = new ObjectInputStream(Files.newInputStream(outputPath))) {
      UdacisearchClient deserialized = (UdacisearchClient) in.readObject();
      System.out.println("Deserialized " + deserialized);
    }
  }
}